package com.cc.controller;

import com.cc.model.EbNews;
import com.cc.model.EbProduct;
import com.cc.model.EbProductCategory;
import com.cc.service.NewsService;
import com.cc.service.ProductCategoryService;
import com.cc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-10-29 16:36
 *
 * web层----servcie层----mapper层
 */
@Controller
@RequestMapping("/index")//迎宾的外号(工号)
public class IndexController {

    @Autowired
    private NewsService  newsService;


    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    //http://localhost:8080/easybuy/index/index?page=2
    @RequestMapping("/index")//方法外号
    //返回首页数据(model)和视图(jsp、html)方法
    public String index(Model model,
                        @RequestParam(value = "page",defaultValue = "1")  int page,
                        @RequestParam(value = "pageSize",defaultValue = "8")int pageSize){
        //一、调用service获取数据
        //1.调用service层分页的新闻数据--新闻表
        List<EbNews> newes = newsService.list(1, 7);
        //2.调用service层分页的商品数据--商品表
        List<EbProduct> products = productService.list(page, pageSize);

        //3.调用service层显示所有商品分类数据---商品分类表
        Map<String, List<EbProductCategory>> categoryMap =
                productCategoryService.findBigAndSmall();

        //二、将数据绑定到model
        //4.将数据绑定到model中  key-vlalue
        model.addAttribute("newes",newes);

        model.addAttribute("products",products);

        model.addAttribute("categoryMap",categoryMap);

        //三、返回视图
        //5.视图(jsp、html)---index.jsp
        //   前缀(/WEB-INF)+ index + 后缀(.jsp)
        return "index";// /WEB-INF/index.jsp

    }



}
